package com.study.servlet;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.study.dao.CustomerDao;
import com.study.dao.impl.CustomerDaoImpl;
import com.study.model.Customer;
import com.study.util.GsonHelper;
import com.study.util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author WS
 */
@WebServlet(name = "queryCustomer",urlPatterns = "/queryCustomer")
public class CustomerQueryServlet extends BaseServlet {
    private CustomerDao dao=new CustomerDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决json中文乱码
        resp.setContentType("text/json;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");

        Customer query=new Customer();
        String curpagestr=req.getParameter("curpage").isEmpty()?"1":req.getParameter("curpage");
        String pagesizestr=req.getParameter("pagesize").isEmpty()?"10":req.getParameter("pagesize");
        int curpage=Integer.parseInt(curpagestr);
        int pagesize=Integer.parseInt(pagesizestr);
        query.setNickname(req.getParameter("nickname").isEmpty()?"":req.getParameter("nickname"));
        query.setName(req.getParameter("name").isEmpty()?"":req.getParameter("name"));
        query.setCardType(req.getParameter("cardtype").isEmpty()?"":req.getParameter("cardtype"));
        query.setCardNo(req.getParameter("cardno").isEmpty()?"":req.getParameter("cardno"));

        PrintWriter out = resp.getWriter();
        PageHelper<Customer> customer=dao.queryAllByPagerHelper(query,curpage,pagesize,req.getParameter("orderBy"));
        String json= GsonHelper.toJsonWithDate(customer);
        out.println(json);
        System.out.println(json);
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req,resp);
        doGet(req, resp);
    }
}
